﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;


namespace Team3_ProjectGUIFinal
{
    class LocateDebug : Algorithm
    {
        public LocateDebug(String name) : base(name) { }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Bitmap returnImage = new Bitmap(sourceImage); // New Bitmap
            Rectangle rect = new Rectangle(0, 0, returnImage.Width, returnImage.Height);
            BitmapData returnImageDate = returnImage.LockBits(rect, ImageLockMode.ReadWrite, returnImage.PixelFormat);
            IntPtr pointer = returnImageDate.Scan0;// Lock Bits and get a pointer to the first Byte.


            int Size = Math.Abs(returnImageDate.Stride) * returnImage.Height; // Number of Bytes
            // Create new Byte Array's and histograms
            byte[] locate = new Byte[Size];
            byte[] median = new Byte[Size];

            int[] Blist = new int[256];

            // Calculate the bytes per pixel and the calculates the radius of the matrix
            int Bytespp = (returnImageDate.Stride / returnImage.Width);
            double Afronding = 3;
            double versterk = 1.8;
            int width = sourceImage.Width;
            int height = sourceImage.Height;
            byte zero = Convert.ToByte(0);
            byte max = Convert.ToByte(253);

            Marshal.Copy(pointer, locate, 0, Size); // Copy Bitmap into the Byte array


            for (int i = 0; i < (Size - Bytespp); i += Bytespp)// Edit every pixel skip every Alpha Byte
            {
                // Enhance greens and set all none yellows to black
                if (locate[i + 1] > locate[i + 2])
                {
                    if (locate[i] * 1.5 > 255) { locate[i] = 255; }
                    else { locate[i] = Convert.ToByte(locate[i] * 1.5); }
                }
                else if (locate[i + 1] > locate[i + 2])
                {
                    if (locate[i] * 0.5 < 0) { locate[i] = 0; }
                    else { locate[i] = Convert.ToByte(locate[i] * 0.5); }
                }

                if ((locate[i] * Afronding < locate[i + 1] && locate[i] * Afronding < locate[i + 2]) == false)
                {
                    locate[i] = zero;
                    locate[i + 1] = zero;
                    locate[i + 2] = zero;   
                }

                if (locate[i + 2] < 165)
                {
                    locate[i] = zero;
                    locate[i + 1] = zero;
                    locate[i + 2] = zero;
                }
            }
            for (int i = 0; i < (Size - Bytespp); i += Bytespp)// Edit every pixel skip every Alpha Byte
            {
                if (locate[i + 2] < 70)
                {

                }
            }

            Marshal.Copy(locate, 0, pointer, Size); //Copy Array back into the Bitmap
            returnImage.UnlockBits(returnImageDate); // Unlock the bits



            int x = 0;
            int y = 0;
            int set = 0;
            int[] Xlist = new int[sourceImage.Width];
            int[] Ylist = new int[sourceImage.Height];
            for (int i = 0; i < (Size - Bytespp); i += Bytespp)
            {
                if (locate[i + 2] > 0)
                {
                    Xlist[x]++;
                    Ylist[y]++;
                    set++;
                }
                x++;
                if (x - width >= 0)
                {
                    x = 0;
                    y++;
                }
            }

            Color Wit = Color.FromArgb(155, 255, 255, 255);
            Color Rood = Color.FromArgb(155, 255, 0, 0);
            Pen pen = new Pen(Wit, 1);

            Graphics gp = Graphics.FromImage(returnImage);

            int hoogste = 0;
            Point links = new Point();
            Point rechts = new Point();
            links.X = 0;
            links.Y = 0;
            rechts.X = 0;
            rechts.Y = 0;

            // zoek hoogste aantal pixels op X as
            for (int i = 0; i < sourceImage.Width - 1; i++)
            {
                pen.Color = Wit;
                rechts.Y = Xlist[i + 1];

                if (hoogste < Xlist[i])
                {
                    pen.Color = Rood;
                    hoogste = Xlist[i];
                    x = i;
                }
                gp.DrawLine(pen, links, rechts);
                links.X++;
                rechts.X++;
            }


            hoogste = 0;

            links.X = 0;
            links.Y = 0;
            rechts.X = 0;
            rechts.Y = 0;
            // zoek hoogste aantal pixels op Y as
            for (int i = 0; i < sourceImage.Height - 1; i++)
            {
                pen.Color = Wit;
                rechts.X = Ylist[i + 1];
                if (hoogste < Ylist[i])
                {
                    pen.Color = Rood;
                    hoogste = Ylist[i];
                    y = i;

                }
                gp.DrawLine(pen, links, rechts);
                links.Y++;
                rechts.Y++;
            }

            int lpwidth = x;
            int lpheight = y;

            //zoek hoogste punt van het nummerboord
            while (y > 3)
            {
                if (Ylist[y] < 5 && Ylist[y - 3] == 0 || y == 0)
                {
                    break;
                }
                y--;
            }

            // bereken de hoogte
            // find the y for the lowerleft corner
            while (lpheight < height)
            {
                if (Ylist[lpheight] < 2)
                {
                    break;
                }
                lpheight++;
            }
            lpheight -= y;

            // zoek meest linkse punt van het nummerboord
            while (x > 5)
            {
                if (Xlist[x] < 5 && Xlist[x - 5] == 0 || x == 0)
                {
                    break;
                }
                x--;
            }

            int empty = 0;
            //find the x for the lowerleft corner
            while (lpwidth < width - 5)
            {
                if (Xlist[lpwidth] <= 1 && Xlist[lpwidth + 5] == 0)
                {
                    if (empty > 6)
                    {
                        break;
                    }
                    empty++;
                }

                lpwidth++;
            }
            lpwidth -= x;

            pen.Color = Rood;
            pen.Width = 10;
            Point Rechts = new Point();
            Rechts.X = 1516;
            Rechts.Y = 1010;
            Point Links = new Point();
            Links.X = 1006;
            Links.Y = 726;
            //getColorAlgorithm get = new getColorAlgorithm("get");
            //Color cl = get.DoAlgorithm(sourceImage, Links, Rechts);
            //MessageBox.Show("Links x: " + x + " y: " + y + " Rechts x:" + (x + breedte) + " y:" + (y + hoogte));
            gp.DrawRectangle(pen, x - 5, y - 5, lpwidth + 10, lpheight + 10);

            return returnImage;
        }
    }
}
